import authApi from '@/service/api/photo/auto'
import photoImageApi from '@/service/api/photo/image'
interface backParams {
  success: number
  error: number
  total: number
}
interface optionsState {
  back: (info: backParams) => void
  success: () => void
}
const getImgArr = async (newName: string, i = 0) => {
  try {
    const { data } = await authApi.fulituImages({
      link: newName
    })
    return data
  } catch (e) {
    if (i === 5) {
      return []
    } else {
      return await getImgArr(newName, i + 1)
    }
  }
}
const getPage = async (href: string, i = 0) => {
  try {
    const { data } = await authApi.getPage({
      link: href
    })
    return data
  } catch (e) {
    if (i === 5) {
      return []
    } else {
      return await getPage(href, i + 1)
    }
  }
}

export function fulituRu(row: any, options: optionsState) {
  const totalPage = Math.ceil(row.total / 30) + 1
  const backInfo: backParams = {
    success: 0,
    error: 0,
    total: row.total
  }
  const handStar = async (i = 1) => {
    if (i === totalPage) {
      options.success()
      return
    }
    const { data } = await authApi.fulitu({
      page: i,
      ...row.queryForm
    })
    handRu(data, i)
  }
  const handRu = async (arr: any, page: number, i = 0) => {
    // if (page === 1 && i === 0) {
    //   i = 5
    // }
    if (i === arr.length) {
      setTimeout(() => {
        handStar(page + 1)
      }, 5000)
      return
    }
    const item = arr[i]
    const data = await getPage(item.href)
    const names = item.href.split('.')
    const requestArr = []
    for (let j = 1; j <= Number(data); j++) {
      const newName = names[0] + '-' + j + '.' + names[1]
      requestArr.push(getImgArr(newName))
    }
    try {
      const res = await Promise.all(requestArr)
      const images = res.flat()
      const params = {
        type_id: row.queryForm.type_id,
        institution_id: row.queryForm.institution_id,
        name: item.title,
        author: item.author
      }
      authApi.pyAdd(params).then(({ data }) => {
        const count = images.length
        let success = 0
        images.forEach((el: string) => {
          photoImageApi
            .add({
              photo_id: data,
              image_url: el,
              sort: 1
            })
            .finally(() => {
              success++
              if (success === count) {
                backInfo.success++
                options.back(backInfo)
                handRu(arr, page, i + 1)
              }
            })
        })
      })
    } catch (e) {
      console.log(e)
    }
  }
  handStar()
}
export function fulituDown(row: any, options: optionsState) {
  const totalPage = Math.ceil(row.total / 30) + 1
  const backInfo: backParams = {
    success: 0,
    error: 0,
    total: row.total
  }
  const handStar = async (i = 1) => {
    if (i === totalPage) {
      options.success()
      return
    }
    const { data } = await authApi.fulitu({
      page: i,
      ...row.queryForm
    })
    handDown(data, i)
  }

  const handDown = async (arr: any, page: number, i = 0) => {
    if (i === arr.length) {
      setTimeout(() => {
        handStar(page + 1)
      }, 5000)
      return
    }
    const item = arr[i]
    const data = await getPage(item.href)
    const names = item.href.split('.')
    const requestArr = []
    for (let j = 1; j <= Number(data); j++) {
      const newName = names[0] + '-' + j + '.' + names[1]
      requestArr.push(getImgArr(newName))
    }
    try {
      const res = await Promise.all(requestArr)
      const images = res.flat()
      authApi
        .saveImages({
          images,
          title: `${page}-${i}-${item.title}`
        })
        .then(() => {
          backInfo.success++
        })
        .catch(() => {
          backInfo.error++
        })
        .finally(() => {
          options.back(backInfo)
        })
      setTimeout(() => {
        handDown(arr, page, i + 1)
      }, 3000)
    } catch (e) {
      setTimeout(() => {
        handDown(arr, page, i)
      }, 5000)
    }
  }
  handStar()
}

const getJkImgArr = async (newName: string, i = 0) => {
  try {
    const { data } = await authApi.jkDetail({
      link: newName
    })
    return data
  } catch (e) {
    if (i === 5) {
      return []
    } else {
      return await getImgArr(newName, i + 1)
    }
  }
}
export function jkDown(row: any, options: optionsState) {
  const totalPage = row.total
  const backInfo: backParams = {
    success: 0,
    error: 0,
    total: row.total
  }
  const handStar = async (i = 1) => {
    if (i === totalPage) {
      options.success()
      return
    }
    const { data } = await authApi.jkList({
      page: i,
      ...row.queryForm
    })
    handDown(data, i)
  }

  const handDown = async (arr: any, page: number, i = 0) => {
    if (i === arr.length) {
      setTimeout(() => {
        handStar(page + 1)
      }, 5000)
      return
    }
    const item = arr[i]
    try {
      const images = await getJkImgArr(item.href)
      authApi
        .jkSave({
          images,
          title: `${page}-${i}-${item.title}`
        })
        .then(() => {
          backInfo.success++
        })
        .catch(() => {
          backInfo.error++
        })
        .finally(() => {
          options.back(backInfo)
        })
      setTimeout(() => {
        handDown(arr, page, i + 1)
      }, 3000)
    } catch (e) {
      setTimeout(() => {
        handDown(arr, page, i)
      }, 5000)
    }
  }
  handStar()
}

export function jkRu(row: any, options: optionsState) {
  const totalPage = row.total
  const backInfo: backParams = {
    success: 0,
    error: 0,
    total: row.total
  }
  const handStar = async (i = 1) => {
    if (i === totalPage) {
      options.success()
      return
    }
    const { data } = await authApi.jkList({
      page: i,
      ...row.queryForm
    })
    handRu(data, i)
  }
  const handRu = async (arr: any, page: number, i = 0) => {
    if (i === arr.length) {
      setTimeout(() => {
        handStar(page + 1)
      }, 5000)
      return
    }
    const item = arr[i]
    try {
      const images = await getJkImgArr(item.href)
      const params = {
        type_id: row.queryForm.type_id,
        institution_id: row.queryForm.institution_id,
        name: item.title,
        author: 'jk.rs'
      }
      authApi.pyAdd(params).then(({ data }) => {
        const count = images.length
        let success = 0
        images.forEach((el: string) => {
          photoImageApi
            .add({
              photo_id: data,
              image_url: el,
              sort: 1
            })
            .finally(() => {
              success++
              if (success === count) {
                backInfo.success++
                options.back(backInfo)
                handRu(arr, page, i + 1)
              }
            })
        })
      })
    } catch (e) {
      console.log(e)
    }
  }
  handStar()
}
